package com.ly.user_system.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.security.core.context.SecurityContextImpl;

// 使用TransmittableThreadLocal存储上下文
public class CusContextStrategy implements SecurityContextHolderStrategy {
    private final static TransmittableThreadLocal<SecurityContext> THREAD_LOCAL =new TransmittableThreadLocal<>();

    @Override
    public void clearContext() {
        THREAD_LOCAL.remove();
    }

    @Override
    public SecurityContext getContext() {
        return THREAD_LOCAL.get();
    }

    @Override
    public void setContext(SecurityContext context) {
        THREAD_LOCAL.set(context);
    }

    @Override
    public SecurityContext createEmptyContext() {
        return new SecurityContextImpl();
    }
}
